package com.maybach.core.flow.handler;

import com.audi.dubbo.api.admin.models.AdminModel;
import com.maybach.common.model.CommonEnumDO;
import com.maybach.common.query.CommonEnumQuery;
import com.maybach.core.dubbo.consumer.adapter.AdminServiceDubooAdapter;
import com.maybach.core.manager.CommonEnumManager;
import com.maybach.core.msg.IMsgService;
import com.maybach.util.CollectionUtil;
import com.maybach.util.Transformer;
import com.maybach.workflow.common.AbstractHanlder;
import com.maybach.workflow.common.api.FlowChain;
import com.maybach.workflow.common.api.IFlowHandler;
import com.maybach.workflow.common.api.WorkFlowContext;
import com.maybach.workflow.common.api.enums.ActorTypeEnum;
import com.maybach.workflow.common.api.node.TaskNode;
import com.maybach.workflow.common.model.FlowTaskActorDO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by houenxun on 16/2/22.
 *
 * 信任务通知
 */
@Component
public class NotifyNewTaskHanlder extends AbstractHanlder implements IFlowHandler{
    @Autowired
    protected CommonEnumManager commonEnumManager;
    @Autowired
    protected IMsgService msgService;
    @Autowired
    AdminServiceDubooAdapter adminServiceDubooAdapter;

    @Override
    public void handle(WorkFlowContext context) {
        // TODO 通知谁的问题  这里先写死 后续再想办法传递参数
        //FlowTaskDO taskDO = context.getThisTask();
        TaskNode  taskNode = (TaskNode)context.getThisNode();
        FlowChain flowChain = context.getThisFlowChain();

        String msg = flowChain.getDisplayName()+"当前节点是:"+taskNode.getDisplayName()+", 请及时进行处理" + "【卖好车】";

        List<FlowTaskActorDO> list =  context.getTaskActorDOList();
        for(FlowTaskActorDO actorDO: list){
           if(ActorTypeEnum.role.name().equals(actorDO.getActorType())&& StringUtils.isNotBlank(actorDO.getValue())){
               List<AdminModel> adminList = adminServiceDubooAdapter.getAdminModelByRoleCode(actorDO.getValue()) ;
               if(CollectionUtils.isNotEmpty(adminList)){
                   for(AdminModel model: adminList){
                       if(StringUtils.isNotBlank(model.getMobile())) {
                           msgService.sendMsg(model.getMobile(), msg);
                       }
                   }
               }

           }
        }
    }


    private List<String> getMobiles(String role){
        CommonEnumQuery query = new CommonEnumQuery();
        CommonEnumQuery.Criteria criteria  = query.or();
        criteria.andGroupCodeEqualTo(role);
        criteria.andStatusEqualTo(0);
        query.setOrderByClause(" sort_order ");
        List<CommonEnumDO> enumDOs = this.commonEnumManager.selectByQuery(query);

        return CollectionUtil.transform(enumDOs, new Transformer<CommonEnumDO, String>() {
            @Override
            public String transform(CommonEnumDO object) {
                return object.getCode();
            }
        });
    }

    @Override
    protected String getHandlerKey() {
        return "notifyNewTask";
    }
}
